//
//  DINGStatusTopView.swift
//  0730Microblog
//
//  Created by ding on 15/8/6.
//  Copyright © 2015年 ding. All rights reserved.
//

import UIKit
import SDWebImage

class DINGStatusTopView: UIView {
    
    var statuses: DINGMicroblogStatus? {
        didSet {
            iconImg.sd_setImageWithURL(NSURL(string: (statuses?.user?.profile_image_url)!))

            nameLabel.text = statuses?.user?.name
            nameLabel.sizeToFit()
            creatTimeLabel.text = "刚刚"
            statusSource.text = statuses?.source
            if statuses?.user?.verifiedImgString != nil {
                certifyImg.image = UIImage(named: (statuses?.user?.verifiedImgString)!)
            }
            
            if statuses?.user?.mbrankImgString != nil {
                vipImg.image = UIImage(named: (statuses?.user?.mbrankImgString)!)
            }
            
//            let df = NSDateFormatter()
//            print(statuses?.created_at)
//            if let date = df.dateFromString((statuses?.created_at)!) {
//                let timeDifference = NSDate.timeIntervalSinceDate(date)
//                print(timeDifference)
//            }
            creatTimeLabel.text = "新鲜出炉的"
            creatTimeLabel.sizeToFit()
            statusSource.text = "出自XXXXX网站"
            statusSource.sizeToFit()
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.clearColor()
        setupTopViewUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupTopViewUI() {
        addSubview(iconImg)
        addSubview(nameLabel)
        addSubview(creatTimeLabel)
        addSubview(statusSource)
        addSubview(vipImg)
        addSubview(certifyImg)
        //MARK: - 布局
        iconImg.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconImg, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 8))
        addConstraint(NSLayoutConstraint(item: iconImg, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 8))
        
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 8))
        addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: iconImg, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 8))
        
        certifyImg.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: certifyImg, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImg, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: certifyImg, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImg, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        vipImg.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: vipImg, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 8))
        addConstraint(NSLayoutConstraint(item: vipImg, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: nameLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 8))
        
        creatTimeLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: creatTimeLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -8))
        addConstraint(NSLayoutConstraint(item: creatTimeLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: iconImg, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 8))
        
        statusSource.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: statusSource, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: creatTimeLabel, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: statusSource, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: creatTimeLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 8))
        
    }
    
    //MARK: - 懒加载控件
    private lazy var iconImg: UIImageView = UIImageView()
    
    private lazy var nameLabel: UILabel = UILabel(tColor: UIColor.darkGrayColor(), fontSize: 18)
    private lazy var creatTimeLabel:UILabel = UILabel(tColor: UIColor.lightGrayColor(), fontSize: 12)
    private lazy var statusSource:UILabel = UILabel(tColor: UIColor.lightGrayColor(), fontSize: 12)
    private lazy var vipImg: UIImageView = UIImageView()
    private lazy var certifyImg: UIImageView = UIImageView()
    
}
